package com.techindata.cacheredis;

import org.apache.log4j.Logger;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @author jixiang
 * 
 *         Redis进行加一操作
 * 
 * **/
public class CacheStr {
	private static Logger logger = Logger.getLogger(CacheStr.class);
	static JedisPool pool = RedisConnectionPool.getPool();
    /**
     * @param str 对指定key增1
     * 
     * 
     * **/
	public static long incr(String str) {
		long num = 0;
		Jedis jedis=null;
		try {
			jedis= pool.getResource();
			num = jedis.incr(str);
		} catch (Exception e) {
            if(jedis!=null){
                jedis.disconnect();
            }
			logger.error(e.getMessage(), e);
			new Exception(e.getMessage());
		}
		finally{
			RedisConnectionPool.returnResource(pool, jedis);
		}
		return num;
	}
	public static long decr(String str) {
		long num = 0;
		Jedis jedis=null;
		try {
			jedis= pool.getResource();
			num = jedis.decr(str);
		} catch (Exception e) {
			if(jedis!=null){
				jedis.disconnect();
			}
			logger.error(e.getMessage(), e);
			new Exception(e.getMessage());
		}
		finally{
			RedisConnectionPool.returnResource(pool, jedis);
		}
		return num;
	}
}
